package com.shangjinrong.controller.site;

import com.shangjinrong.domain.TransFlow;
import com.shangjinrong.result.R;
import com.shangjinrong.service.TransFlowService;
import com.shangjinrong.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("site/transFlow")
@Slf4j
public class SiteTransFlowController {
    @Autowired
    private TransFlowService transFlowService;

    @GetMapping("select")
    public R list (){
        List<TransFlow> list = transFlowService.list();

        return R.ok().message("查询成功").data("list",list);
    }

    @PostMapping("insert")
    public R insertTransFlow(@RequestBody TransFlow transFlow) {
        boolean save = transFlowService.save(transFlow);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @GetMapping("selectFundingById")
    public R list(HttpServletRequest request) {
        String token = request.getHeader("token");
        long userId = JwtUtils.getUserId(token);
        List<TransFlow> transFlows = transFlowService.selectFunding(userId);

        return R.ok().message("查询成功").data("transFlows", transFlows);
    }


}
